package com.glorysoft.tonglingxjpda.ui.function.view

import android.os.Bundle
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import com.glorysoft.tonglingxjpda.BR
import com.glorysoft.tonglingxjpda.R
import com.glorysoft.tonglingxjpda.base.BaseActivity
import com.glorysoft.tonglingxjpda.databinding.ActivityDataCollectionBinding
import com.glorysoft.tonglingxjpda.entity.BatchListEntity
import com.glorysoft.tonglingxjpda.entity.ui.ToastMessageBean
import com.glorysoft.tonglingxjpda.ui.function.viewmodel.DataCollectionViewModel
import kotlinx.android.synthetic.main.activity_data_collection.*

/**
 * FileName: DataCollectionActivity
 * Author: XJH
 * Date: 2022/5/6 17:20
 * Description:
 * History:
 */
class DataCollectionActivity : BaseActivity<DataCollectionViewModel,ActivityDataCollectionBinding>(),
    View.OnClickListener {
    override fun layoutId(): Int {
        return R.layout.activity_data_collection
    }

    override fun initVariableId(): Int {
        return BR.dataCollectionViewModel
    }

    override fun initView(savedInstanceState: Bundle?) {
        setTitle("数据采集")
//        val batchID = intent.getStringExtra("BatchID")
//        val LOTID = intent.getStringExtra("LOTID")
        val data = intent.getSerializableExtra("DATA") as BatchListEntity.BatchInfoEntity
        tvBatchID.text = data.BATCHID
        tvLotID.text = data.LOTID

        rvData.layoutManager = LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL,false)
        viewModel.getEdcData(data.BATCHID,data.LOTID)
        rvData.adapter = viewModel.mAdapter
    }

    override fun initData() {
        tvTempSave.setOnClickListener(this)
        tvSave.setOnClickListener(this)

        viewModel.defUI.startActivity.observe(this){
            viewModel.defUI.toastEvent.value = ToastMessageBean("操作成功",true)
            setResult(RESULT_OK)
            finish()
        }
    }

    override fun onClick(v: View?) {
        if (viewModel.edcInfo == null){
            viewModel.defUI.toastEvent.value = ToastMessageBean("未获取到采集项集",false)
            return
        }
        when(v?.id){
            R.id.tvTempSave -> viewModel.saveEdcData(true)
            R.id.tvSave -> viewModel.saveEdcData(false)
        }
    }
}